package org.opens.util;

import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2022/10/12
 */
public class JSvnUtil {

    public static void getLogByMessage(String svnUrl, String userName, String password, String message) throws Exception {
        SVNClientManager svnClient = JSvnUtil.getSvnClientManager(svnUrl, userName, password);

        SVNLogClient logClient = svnClient.getLogClient();

        List<SVNLogEntryPath> result = new ArrayList<>();
        long version = 32808L;

        SVNURL url = SVNURL.parseURIEncoded(svnUrl);
        String[] paths = { "." };
        SVNRevision pegRevision = SVNRevision.create( version );
        SVNRevision startRevision = SVNRevision.create( version );
        SVNRevision endRevision = SVNRevision.create( version );
        boolean stopOnCopy = false;
        boolean discoverChangedPaths = true;
        long limit = 9999l;

        ISVNLogEntryHandler handler = new ISVNLogEntryHandler() {

            /**
             * This method will process when doLog() is done
             */
            @Override
            public void handleLogEntry( SVNLogEntry logEntry ) throws SVNException {
                System.out.println( "Author: " + logEntry.getAuthor() );
                System.out.println( "Date: " + logEntry.getDate() );
                System.out.println( "Message: " + logEntry.getMessage() );
                System.out.println( "Revision: " + logEntry.getRevision() );
                System.out.println("-------------------------");
                Map<String, SVNLogEntryPath> maps = logEntry.getChangedPaths();
                Set<Map.Entry<String, SVNLogEntryPath>> entries = maps.entrySet();
                for(Map.Entry<String, SVNLogEntryPath> entry : entries){
                    //System.out.println(entry.getKey());
                    SVNLogEntryPath entryPath = entry.getValue();
                    result.add(entryPath);
                    System.out.println(entryPath.getType()+" "+entryPath.getPath());
                }
            }
        };
        try {
            logClient.doLog( url, paths, pegRevision, startRevision, endRevision, stopOnCopy, discoverChangedPaths, limit, handler );
        }
        catch ( SVNException e ) {
            System.out.println( "Error in doLog() " );
            e.printStackTrace();
        }
        System.out.println(result);
    }

    public static SVNClientManager getSvnClientManager(String svnUrl, String userName, String password) {
        try {
            JSvnUtil.setupLibrary();

            SVNURL svnurl = SVNURL.parseURIEncoded(svnUrl);

            ISVNOptions options = SVNWCUtil.createDefaultOptions(true);

            SVNClientManager svnClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password);
            return svnClientManager;
        } catch (SVNException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     * 初始化库
     */
    private static void setupLibrary() {
        /*
         * For using over http:// and https://
         */
        DAVRepositoryFactory.setup();
        /*
         * For using over svn:// and svn+xxx://
         */
        SVNRepositoryFactoryImpl.setup();

        /*
         * For using over file:///
         */
        FSRepositoryFactory.setup();
    }

}
